﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Billing.Windows;
using Billing.Dtos;
using System.Data;

namespace Billing.Utilities
{
    public static class CacheManager
    {
        public static List<BillingGridItemDto> BillingItems;

        public static void LoadItems()
        {
            if (BillingItems == null || BillingItems.Count < 1)
            {
                CacheManager.BillingItems = new List<BillingGridItemDto>();
                DataTable dt = Database.GetAllBillingItems();

                foreach (DataRow dr in dt.Rows)
                {
                    BillingGridItemDto billingItem = new BillingGridItemDto();
                    billingItem.Code = Convert.ToInt32(dr["ItemCode"].ToString());
                    billingItem.Name = dr["ItemName"].ToString();
                    billingItem.Price = double.Parse(dr["SellingPrice"].ToString());
                    billingItem.Dept = dr["Dept"].ToString();
                    billingItem.AcceptNegativeStock = bool.Parse(dr["AcceptNegativeStock"].ToString());
                    billingItem.AcceptDecimalStock = bool.Parse(dr["AcceptDecimalStock"].ToString());
                    billingItem.PopupLimit = double.Parse(dr["PopupLimit"].ToString());
                    billingItem.IsStockShown = bool.Parse(dr["IsLessStockShown"].ToString());
                    try
                    {
                        //For Cake Balances - 1000 code
                        billingItem.Stock = double.Parse(dr["StockAvailable"].ToString());
                    }
                    catch (Exception) { }
                    billingItem.GroupID = dr["GroupID"].ToString();
                    billingItem.IsSlipRequired = bool.Parse(dr["IsSlipRequired"].ToString());
                    billingItem.VAT = dr["VAT"].ToString();
                    BillingItems.Add(billingItem);
                }
            }
        }

        public static void ResetCacheManager()
        {
            BillingItems.Clear();
            LoadItems();
        }
    }
}
